package com.taptech.doufu.ui.view.personalcenter;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.taptech.doufu.util.ScreenUtil2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LongSiyang on 2015/5/13.
 */
public class TagsViewGroup extends ViewGroup
{

    private int mChildCount;
    public static int WIDTH_SPACE_DP = 10;
    public static int HEIGHT_SPACE_DP = 8;
    private int mWidthSpace = ScreenUtil2.dip2px(WIDTH_SPACE_DP);
    private int mHeightSpace = ScreenUtil2.dip2px(HEIGHT_SPACE_DP);
    private int mChildBackGround = -1;

    protected int mMaxTextSize = 30;
    protected int mMinTextSize = 15;
    // 用来测量
    protected TextView mMaxTextView;

    protected Context mContext;

    // 标签布局模式选项
    public static final int REGULAR_MODE = 0;
    public static final int COORDINATE_MODE = 1;
    public static final int CLOUD_MODE = 2;
    // 标签的布局模式
    protected int mLayoutMode = REGULAR_MODE;
    ;

    // 动画模式选项
    public static final int VERTICLE_ENTRY = 0;
    public static final int HORIZON_ENTRY = 1;
    public static final int BOTTOM_ENTRY = 2;
    // 动画模式
    private int mAnimationMode = -1;

    // 矩形框
    protected class mRectangle
    {
        int ml, mt, mr, mb;

        public boolean isInTheRectangle(int x, int y)
        {
            if (x >= ml && x <= mr && y >= mt && y <= mb)
            {
                return true;
            } else
                return false;
        }

        public boolean isIncludeTheRectangle(int l, int t, int r, int b)
        {
            if (ml >= l && ml <= r && mt >= t && mt <= b)
            {
                return true;
            }
            if (ml >= l && ml <= r && mb >= t && mb <= b)
            {
                return true;
            }
            if (mr >= l && mr <= r && mt >= t && mt <= b)
            {
                return true;
            }
            if (mr >= l && mr <= r && mb >= t && mb <= b)
            {
                return true;
            }
            return false;

        }

        public boolean isRectangleShadowRectangle(int l, int t, int r, int b)
        {
            if (l > mr || r < ml || b < mt || t > mb)
            {
                return false;
            }
            // 斜角覆盖
            if (isInTheRectangle(l, t) || isInTheRectangle(l, b) || isInTheRectangle(r, t) || isInTheRectangle(r, b))
            {
                return true;
            }
            // 反斜角覆盖
            if (isIncludeTheRectangle(l, t, r, b))
            {
                return true;
            }

			/*
             *
			 * // 侧边覆盖 if ((((l <= mr) && (l >= ml)) || ((r <= mr) && (r >=
			 * ml))) && (((t <= mt) && (b >= mb)) || ((t >= mt) && (b <= mb))))
			 * { return true;
			 * 
			 * } //底边覆盖 if ((((t <= mb) && (t >= mt)) || ((b <= mb) && (b >=
			 * mt))) && (((l <= ml) && (r >= mr)) || ((l >= ml) && (r <= mr))))
			 * { return true; } /// 斜角覆盖 if ((((l <= mr) && (l >= ml)) || ((r <=
			 * mr) && (r >= ml))) && (((b <= mb) && (b >= mt) || (t <= mb) && (t
			 * >= mt)))) { return true; }
			 */
            return false;
        }
    }

    protected List<mRectangle> mRectangleList;
    private TranslateAnimation showTranslateAnimation;

    public TagsViewGroup(Context context)
    {
        super(context);
        initView(context);
    }

    public TagsViewGroup(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initView(context);
    }

    public TagsViewGroup(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context)
    {
        this.mContext = context;
        mRectangleList = new ArrayList<mRectangle>();
    }

    @Override
    public void addView(View child)
    {
        if (mChildBackGround != -1)
        {
            child.setBackgroundResource(mChildBackGround);
        }
        super.addView(child);
        //		showTranslateAnimation = new TranslateAnimation(-100, 0, 0, 0);
        //		showTranslateAnimation.setDuration(2000);
        //		child.setAnimation(showTranslateAnimation);
        //		showTranslateAnimation.startNow();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mChildCount = getChildCount();
        View childView;
        for (int i = 0; i < mChildCount; i++)
        {
            childView = getChildAt(i);
            measureChild(childView, MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        }

    }


    /**
     * @param l Left position, relative to parent
     * @param t Top position, relative to parent
     * @param r Right position, relative to parent
     * @param b Bottom position, relative to parent
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        View childView;
        int childWidth = 0;
        int childHeight = 0;

        if (true)
        {
            switch (mLayoutMode)
            {
                // 从左到右、从上到下的顺序布局
                case REGULAR_MODE:
                {
                    int row = 0;
                    int num = 0;
                    int parentWidthLeave = this.getMeasuredWidth();

                    for (int i = 0; i < mChildCount; i++)
                    {

                        childView = getChildAt(i);
                        childWidth = childView.getMeasuredWidth() + mWidthSpace;
                        childHeight = childView.getMeasuredHeight() + mHeightSpace;

                        // 此处只计算控件是否能放下，忽略右边的空格
                        if (childWidth > parentWidthLeave)
                        {
                            row++;
                            num = 0;
                            parentWidthLeave = this.getMeasuredWidth();
                        }
                        childView.layout(this.getMeasuredWidth() - parentWidthLeave + mWidthSpace, childHeight * row + mHeightSpace, this.getMeasuredWidth() - parentWidthLeave + childWidth, childHeight * (row + 1));
                        num++;
                        parentWidthLeave -= childWidth;
                    }
                    entryAnimation();
                    break;
                }
                // 指定坐标布局
                case COORDINATE_MODE:
                {
                    for (int i = 0; i < mChildCount; i++)
                    {
                        childView = getChildAt(i);
                        childView.layout((int) childView.getPivotX(), (int) childView.getPivotY(), (int) childView.getPivotX() + childView.getMeasuredWidth(), (int) childView.getPivotY() + childView.getMeasuredHeight());
                    }
                    entryAnimation();
                    break;
                }
                case CLOUD_MODE:
                {
                    for (int i = 0; i < mChildCount; i++)
                    {
                        childView = getChildAt(i);
                        childView.layout(mRectangleList.get(i).ml, mRectangleList.get(i).mt, mRectangleList.get(i).mr, mRectangleList.get(i).mb);
                        Log.d("tag", "llll:" + mRectangleList.get(i).ml + "," + mRectangleList.get(i).mt + "," + mRectangleList.get(i).mr + "," + mRectangleList.get(i).mb);
                    }
                    entryAnimation();
                    break;
                }
            }
        }
    }

    public void addTextViewByXY(String string, float x, float y, int size)
    {
        if (string.equals(""))
        {
            return;
        }
        int dx, dy;
        dx = ScreenUtil2.dip2px(x);
        dy = ScreenUtil2.dip2px(y);
        TextView textView = new TextView(mContext);
        textView.setText(string);
        textView.setPivotX(dx);
        textView.setPivotY(dy);
        textView.setTextSize(size);
        addView(textView);
    }

    public void addTextViewByXY(String string, float x, float y, int size, int alpha)
    {
        if (string.equals(""))
        {
            return;
        }
        int dx, dy;
        dx = ScreenUtil2.dip2px(x);
        dy = ScreenUtil2.dip2px(y);
        TextView textView = new TextView(mContext);
        textView.setText(string);
        textView.setPivotX(dx);
        textView.setPivotY(dy);
        textView.setTextSize(size);
        textView.setTextColor(Color.argb(alpha, 129, 140, 203));
        addView(textView);
    }

    public void entryAnimation()
    {

        View childView;
        mChildCount = getChildCount();
        switch (mAnimationMode)
        {
            case TagsViewGroup.VERTICLE_ENTRY:
            {
                for (int i = 0; i < mChildCount; i++)
                {
                    childView = getChildAt(i);
                    Animation animation = new TranslateAnimation(childView.getPivotX(), childView.getPivotX(), this.getMeasuredHeight(), childView.getPivotY());
                    animation.setDuration(500);
                    animation.setStartOffset(70 * i);
                    childView.startAnimation(animation);
                }
                break;
            }
            case TagsViewGroup.HORIZON_ENTRY:
            {
                for (int i = 0; i < mChildCount; i++)
                {
                    childView = getChildAt(i);
                    Animation animation = new TranslateAnimation(this.getMeasuredWidth(), childView.getPivotX(), childView.getPivotY(), childView.getPivotY());
                    animation.setDuration(500);
                    animation.setStartOffset(70 * i);
                    childView.startAnimation(animation);
                }
                break;
            }
            case TagsViewGroup.BOTTOM_ENTRY:
            {

                for (int i = 0; i < mChildCount; i++)
                {
                    childView = getChildAt(i);
                    AnimationSet animationSet = new AnimationSet(true);
                    Animation animationScale = new ScaleAnimation(0, 1, 0, 1, childView.getWidth() / 2, childView.getHeight() / 2);
                    animationSet.addAnimation(animationScale);
                    animationSet.setDuration(500);
                    animationSet.setStartOffset(100 * i);
                    childView.startAnimation(animationSet);
                }

                break;
            }
        }

    }

    /**
     * 每增加一个child就测量一次，并计算出相应的
     */
    public void addView(View child, mRectangle rectangle)
    {
        // mRectangleList.add(rectangle);
        super.addView(child);
    }

    public int getmChildCount()
    {
        return mChildCount;
    }

    public int getmWidthSpace()
    {
        return mWidthSpace;
    }

    public void setmWidthSpace(int mWidthSpace)
    {
        this.mWidthSpace = mWidthSpace;
    }

    public int getmHeightSpace()
    {
        return mHeightSpace;
    }

    public void setmHeightSpace(int mHeightSpace)
    {
        this.mHeightSpace = mHeightSpace;
    }

    public int getmChildBackGround()
    {
        return mChildBackGround;
    }

    public void setmChildBackGround(int mChildBackGround)
    {
        this.mChildBackGround = mChildBackGround;
    }

    public void setmLayoutMode(int mLayoutMode)
    {
        this.mLayoutMode = mLayoutMode;
    }

    public void setmAnimationMode(int mAnimationMode)
    {
        this.mAnimationMode = mAnimationMode;
    }
}
